package com.crazyhat.crazyhat.utils;

import java.util.Date;
import java.util.Map;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

/**
 * JWT加解密
 */
public class JwtUtil {

    /**
     * 密钥
     */
    private static final String signKeys = "zfzfyslfhs";
    
    /**
     * 有效时间，单位：ms
     */
    private static final long validTime = 1000 * 60 * 60 * 3;
	
	/**
     * 接收业务数据,生成token并返回
     * @param claims
     * @return
     */
    public static String generateToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("claims", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + validTime))
                .sign(Algorithm.HMAC256(signKeys));
    }

	/**
     * 接收token,验证token,并返回业务数据
     * @param token
     * @return
     */
    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(signKeys))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }

}
